package com.beemans.topon.demo.ui.fragments

import android.util.Log
import androidx.core.view.isVisible
import com.anythink.core.api.ATAdInfo
import com.anythink.core.api.AdError
import com.beemans.common.ui.fragments.CommonFragment
import com.beemans.common.utils.ToastUtils
import com.beemans.topon.ads2.AdvertManager
import com.beemans.topon.ads2.SplashAdvert
import com.beemans.topon.ads2.SplashAdvertCallback
import com.beemans.topon.ads2.SplashAdvertConfig
import com.beemans.topon.demo.R
import com.beemans.topon.demo.constant.Constant
import com.beemans.topon.demo.databinding.FragmentSplashBinding
import com.tiamosu.databinding.delegate.lazyDataBindingOrNull
import com.tiamosu.databinding.page.DataBindingConfig
import com.tiamosu.fly.ext.clickNoRepeat

/**
 * @author tiamosu
 * @date 2020/7/9.
 */
class SplashAdFragment : CommonFragment() {
    private val dataBinding: FragmentSplashBinding? by lazyDataBindingOrNull()
//    private val splashAdConfig by lazy { TpSplashAd.TpSplashAdConfig(Constant.SPLASH_ID) }

    override fun getDataBindingConfig(): DataBindingConfig {
        return DataBindingConfig(R.layout.fragment_splash)
    }

    override fun initEvent() {
        // 加载广告
        dataBinding?.splashAdBtnPreload?.clickNoRepeat {
            preloadAdvert()

//            val config = splashAdConfig.deepClone()?.apply { isOnlyPreload = true }
//                ?: return@setOnClickListener
//            TpSplashAd.start(this, config) {
//                onAdLoaded {
//                    val cache = TopOn.getSplashAdCaches(this@SplashAdFragment, config.placementId)
//                    Log.e("susu2", "cache:${cache.size}")
//                }
//            }
        }

        // 展示广告
        dataBinding?.splashAdBtnLoad?.clickNoRepeat {
            showAdvert()

//            dataBinding?.splashAdFlAd?.loadAd(this, splashAdConfig) {
//                onAdShow {
//                    Log.e("susu2", "childCount:${dataBinding?.splashAdFlAd?.childCount}")
//                }
//                onAdLoaded {
//                    dataBinding?.splashAdGroupBtn?.isVisible = false
//                }
//                onAdClose {
//                    dataBinding?.splashAdGroupBtn?.isVisible = true
//                }
//            }
        }
    }

    /**
     * 加载广告
     */
    private fun preloadAdvert() {
        val advertConfig = SplashAdvertConfig(Constant.SPLASH_ID)

        AdvertManager.loadSplashAdvert(
            this,
            advertConfig,
            null,
            object : SplashAdvertCallback() {
                override fun onAdvertLoaded(splashAdvert: SplashAdvert) {
                    printLog("广告加载成功")
                }

                override fun onAdvertLoadFail(splashAdvert: SplashAdvert, adError: AdError?) {
                    printLog("广告加载失败")
                }
            })
    }

    /**
     * 展示广告
     */
    private fun showAdvert() {
        val advertConfig = SplashAdvertConfig(Constant.SPLASH_ID)
        advertConfig.isShowAfterLoaded = true
        advertConfig.isPreloadAdvert = true
        advertConfig.isPreloadWithoutReady = true

        AdvertManager.loadSplashAdvert(
            this,
            advertConfig,
            dataBinding?.splashAdFlAd,
            object : SplashAdvertCallback() {
                override fun onAdvertLoaded(splashAdvert: SplashAdvert) {
                    printLog("广告加载成功")

                    dataBinding?.splashAdGroupBtn?.isVisible = false
                }

                override fun onAdvertLoadFail(splashAdvert: SplashAdvert, adError: AdError?) {
                    printLog("广告加载失败")
                }

                override fun onAdvertShow(splashAdvert: SplashAdvert, atAdInfo: ATAdInfo?) {
                    printLog("广告展示")
                }

                override fun onAdvertClicked(splashAdvert: SplashAdvert, atAdInfo: ATAdInfo?) {
                    printLog("广告点击")
                }

                override fun onAdvertClose(
                    splashAdvert: SplashAdvert,
                    atAdInfo: ATAdInfo?
                ): Boolean {
                    printLog("广告关闭")

                    dataBinding?.splashAdGroupBtn?.isVisible = true
                    return true
                }
            })
    }

    private fun printLog(msg: String) {
        Log.e("susu", msg)
        ToastUtils.showShort(msg)
    }

    override fun doBusiness() {
    }
}